package com.kerrykidz.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kerrykidz.system.dao.mapper.ReportEntityMapper;
import com.kerrykidz.system.dao.model.ReportEntity;
import com.kerrykidz.system.service.IReportService;
import com.kerrykidz.system.util.DateUtil;
import com.kerrykidz.system.util.FileUtil;
import com.kerrykidz.system.util.JXLSUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.ReportSearchParam;
import com.kerrykidz.system.vo.UserInfo;
import com.rdp.framework.exception.RDPException;
/**
 * 
 * @ClassName: ReportServiceImpl 
 * @description: 报表管理的接口实现类
 * @author: zhuss 
 * @date: 2014年9月11日 下午4:29:08 
 * @version: V1.0
 *
 */
@Service
@Transactional
public class ReportServiceImpl implements IReportService{

	@Autowired
	private ReportEntityMapper reportEntityMapper;
	
	/**
	 * 查询数据
	 *
	 * @param: @param reportParam
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: ReportServiceImpl
	 * @throws
	 */
	@Override
	public ReportSearchParam queryData(ReportSearchParam reportParam) throws RDPException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("salerId", reportParam.getSalerId());
		map.put("searchTime", reportParam.getSearchTime());
		reportParam.setParams(map);
		List<?> list = null;
		reportParam.setIspaging(true);
		if(reportParam.getReportName().equals("dailyIncome")){//日常收益
			if(StringUtils.isBlank(reportParam.getSearchTime())){
				reportParam.setSearchTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
				map.put("searchTime", reportParam.getSearchTime());
			}
			//日常收益 - 会籍
			List<ReportEntity> memList = reportEntityMapper.dailyIncomeMem(reportParam);
			reportParam.setMemSize(memList.size());
			reportParam.setMemMap(groupMap(memList));
			//日常收益 - 售票
			List<ReportEntity> ticketList = reportEntityMapper.dailyIncomeTicket(reportParam);
			reportParam.setTicketList(ticketList);
			//日常收益 - 派对团体
			List<ReportEntity> pgList = reportEntityMapper.dailyIncomePG(reportParam);
			reportParam.setPgSize(pgList.size());
			reportParam.setPgMap(groupMap(pgList));
			//日常收益 - 课程
			List<ReportEntity> courseList = reportEntityMapper.dailyIncomeCourse(reportParam);
			reportParam.setCourseList(courseList);
			//日常收益 - 普通商品
			List<ReportEntity> goodsList = reportEntityMapper.dailyIncomeGoods(reportParam);
			reportParam.setGoodsList(goodsList);
			//日常收益 - 其他
			List<ReportEntity> otherList = reportEntityMapper.dailyIncomeOther(reportParam);
			reportParam.setOtherList(otherList);
		}else if(reportParam.getReportName().equals("membershipSale")){//会籍销售统计
			if(StringUtils.isBlank(reportParam.getSearchTime())){
				reportParam.setSearchTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
				map.put("searchTime", reportParam.getSearchTime());
			}
			list = reportEntityMapper.membershipSale(reportParam);
		}else if(reportParam.getReportName().equals("membership")){//会籍统计
			reportParam.setIspaging(false);
			if(StringUtils.isBlank(reportParam.getSearchTime())){
				reportParam.setSearchTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
				map.put("searchTime", reportParam.getSearchTime());
			}
			list = reportEntityMapper.membership(reportParam);
		}else if (reportParam.getReportName().equals("partyOrGroup")){//预约团体统计
			reportParam.setIspaging(false);
			if(StringUtils.isBlank(reportParam.getSearchTime())){
				reportParam.setSearchTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
				map.put("searchTime", reportParam.getSearchTime());
			}else{
				if(reportParam.getSearchTime().length()<8){
					reportParam.setSearchTime(reportParam.getSearchTime()+".01");
					map.put("searchTime", reportParam.getSearchTime());
				}
			}
			list = reportEntityMapper.partyOrGroup(reportParam);
		}else if (reportParam.getReportName().equals("scmCount")){//进场人数
			reportParam.setIspaging(false);
			if(StringUtils.isBlank(reportParam.getSearchTime())){
				reportParam.setSearchTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
				map.put("searchTime", reportParam.getSearchTime());
			}
			list = reportEntityMapper.scmCount(reportParam);
		}else if (reportParam.getReportName().equals("membershipProfit")){//会籍利润
			reportParam.setIspaging(false);
			if(StringUtils.isBlank(reportParam.getSearchTime())){
				reportParam.setSearchTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
				map.put("searchTime", reportParam.getSearchTime());
			}else{
				if(reportParam.getSearchTime().trim().length()<8){
					reportParam.setSearchTime(reportParam.getSearchTime()+".01");
					map.put("searchTime", reportParam.getSearchTime());
				}
			}
			list = reportEntityMapper.membershipProfit(reportParam);
			Map<String, List<ReportEntity>> mapList = groupMap(list);
			reportParam.setMap(mapList);
		}else if (reportParam.getReportName().equals("membershipValue")){//会员储值
			list = reportEntityMapper.membershipValue(reportParam);
		}else if (reportParam.getReportName().equals("partyPrepaid")) {// 派对预付
			list = reportEntityMapper.partyPrepaid(reportParam);
		} else if (reportParam.getReportName().equals("failureMember")) {// 失效会员
			list = reportEntityMapper.failureMember(reportParam);
		}
		reportParam.setResults(list);
		return reportParam;
	}
	
	/**
	 * 
	 * 报表导出
	 * @param: reportParam
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: ReportServiceImpl
	 * @throws
	 */
	@Override
	public void reportExport(ReportSearchParam reportParam,HttpServletResponse response)
			throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		reportParam.setCountedTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
		reportParam.setCountedUser(user.getName());
		reportParam = queryData(reportParam);
		reportParam.setList(reportParam.getResults());
		Map<String,Object> beans = new HashMap<String,Object>();
		beans.put("report", reportParam);
		String destFile = JXLSUtil.exportExcel(reportParam.getReportName(), beans);
		try {
			FileUtil.downLoadFile(destFile, response);
		} catch (Exception e) {
			throw new RDPException("导出文件失败");
		}
	}
	/**
	 * 
	 * 条码导出
	 * @param: barCodeExport
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: ReportServiceImpl
	 * @throws
	 */
	@Override
	public void barCodeExport(ReportSearchParam reportParam,HttpServletResponse response)
			throws RDPException {
		Map<String,Object> beans = new HashMap<String,Object>();
		beans.put("report", reportParam);
		String destFile = JXLSUtil.exportExcel(reportParam.getReportName(), beans);
		try {
			FileUtil.downLoadFile(destFile, response);
		} catch (Exception e) {
			throw new RDPException("导出文件失败");
		}
	}
	
	
	/**
	 * List中数据分组 按照type
	 * @param list
	 * @return
	 * @date: 2014年10月30日
	 */
	public Map<String, List<ReportEntity>> groupMap(List<?> list){
		//定义一个map集合用于分组
		Map<String, List<ReportEntity>> mapList = new HashMap<String, List<ReportEntity>>();
		for (Iterator<?> it = list.iterator(); it.hasNext();){
			ReportEntity re = (ReportEntity)it.next();
			
			if(re.getType().equals("6")||re.getType().equals("12"))
				re.setType(re.getType()+"个月");
			//如果在这个map中包含有相同的键，这创建一个集合将其存起来
			if (mapList.containsKey(re.getType())){
				List<ReportEntity> res = mapList.get(re.getType());
				res.add(re);
			} else{
				List<ReportEntity> newRe = new ArrayList<ReportEntity>();
				newRe.add(re);
				mapList.put(re.getType(), newRe);
			}
		} 
		return mapList;
	}
}
